package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesEquipmentRepairRpcService;
import com.hunttown.mes.rpc.domain.MesEquipmentRepairDTO;
import com.hunttown.mes.rpc.domain.query.MesEquipmentRepairDTOQuery;
import com.hunttown.mes.service.MesEquipmentRepairService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesEquipmentRepairConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:26:18
 */
@Service
public class MesEquipmentRepairRpcServiceImpl implements MesEquipmentRepairRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesEquipmentRepairRpcServiceImpl.class);

    private final MesEquipmentRepairService mesEquipmentRepairService;

    @Autowired
    public MesEquipmentRepairRpcServiceImpl(MesEquipmentRepairService mesEquipmentRepairService) {
        this.mesEquipmentRepairService = mesEquipmentRepairService;
    }

    @Override
    public MesEquipmentRepairDTO insert(MesEquipmentRepairDTO mesEquipmentRepairDTO) {
        try {
            return convert(mesEquipmentRepairService.insert(convert(mesEquipmentRepairDTO)));
        } catch (Exception e) {
            logger.error("MesEquipmentRepair create failed.", e);
            throw new RuntimeException("MesEquipmentRepair create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesEquipmentRepairDTO mesEquipmentRepairDTO) {
        try {
            return mesEquipmentRepairService.updateInfoById(convert(mesEquipmentRepairDTO));
        } catch (Exception e) {
            logger.error("MesEquipmentRepair update by id failed.", e);
            throw new RuntimeException("MesEquipmentRepair update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesEquipmentRepairDTOQuery mesEquipmentRepairDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesEquipmentRepairDTOQuery.getW_id() != null && mesEquipmentRepairDTOQuery.getW_id() > 0) {
                query.put("w_id", mesEquipmentRepairDTOQuery.getW_id());
            }

            if (mesEquipmentRepairDTOQuery.getIds() != null && !mesEquipmentRepairDTOQuery.getIds().equals("")) {
                query.put("ids", mesEquipmentRepairDTOQuery.getIds());
            }

            if (mesEquipmentRepairDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesEquipmentRepairDTOQuery.getStopFlag());
            }

            if (mesEquipmentRepairDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesEquipmentRepairDTOQuery.getDeleteFlag());
            }

            return mesEquipmentRepairService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesEquipmentRepair update by query failed.", e);
            throw new RuntimeException("MesEquipmentRepair update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesEquipmentRepairDTO selectById(Integer id) {
        try {
            return convert(mesEquipmentRepairService.selectById(id));
        } catch (Exception e) {
            logger.error("MesEquipmentRepair select by id failed.", e);
            throw new RuntimeException("MesEquipmentRepair select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesEquipmentRepairDTO selectObjByQuery(MesEquipmentRepairDTOQuery mesEquipmentRepairDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesEquipmentRepairDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesEquipmentRepairDTOQuery.getStopFlag());
            }

            if (mesEquipmentRepairDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesEquipmentRepairDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesEquipmentRepairDTOQuery.getOrderBy())) {
                query.put("orderBy", mesEquipmentRepairDTOQuery.getOrderBy());
            }

            return convert(mesEquipmentRepairService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesEquipmentRepair select obj by query failed.", e);
            throw new RuntimeException("MesEquipmentRepair select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesEquipmentRepairDTO> getListForPage(MesEquipmentRepairDTOQuery mesEquipmentRepairDTOQuery) {
        Query query = Query.page(mesEquipmentRepairDTOQuery.getPn(), mesEquipmentRepairDTOQuery.getPageSize());

        if (mesEquipmentRepairDTOQuery.getId() != null && mesEquipmentRepairDTOQuery.getId() > 0) {
            query.put("id", mesEquipmentRepairDTOQuery.getId());
        }

        if (mesEquipmentRepairDTOQuery.getIds() != null && !mesEquipmentRepairDTOQuery.getIds().equals("")) {
            query.put("ids", mesEquipmentRepairDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesEquipmentRepairDTOQuery.getLikeName())) {
            query.put("likeName", mesEquipmentRepairDTOQuery.getLikeName());
        }

        if (mesEquipmentRepairDTOQuery.getEquipId() != null && mesEquipmentRepairDTOQuery.getEquipId() != -999) {
            query.put("equipId", mesEquipmentRepairDTOQuery.getEquipId());
        }

        if (StringUtils.isNotBlank(mesEquipmentRepairDTOQuery.getShowDate())) {
            query.put("showDate", mesEquipmentRepairDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesEquipmentRepairDTOQuery.getStartDate())) {
            query.put("startDate", mesEquipmentRepairDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesEquipmentRepairDTOQuery.getEndDate())) {
            query.put("endDate", mesEquipmentRepairDTOQuery.getEndDate());
        }

        if (mesEquipmentRepairDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesEquipmentRepairDTOQuery.getStopFlag());
        }

        if (mesEquipmentRepairDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesEquipmentRepairDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesEquipmentRepairDTOQuery.getOrderBy())) {
            query.put("orderBy", mesEquipmentRepairDTOQuery.getOrderBy());
        }

        List<MesEquipmentRepairDTO> list = convert(mesEquipmentRepairService.selectByQuery(query));
        Page<MesEquipmentRepairDTO> page = Page.from(mesEquipmentRepairDTOQuery.getPn(), mesEquipmentRepairDTOQuery.getPageSize(), list);
        page.setTotalItems(mesEquipmentRepairService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesEquipmentRepairService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesEquipmentRepair delete failed.", e);
            throw new RuntimeException("MesEquipmentRepair delete failed, Please order your code.", e);
        }
    }
}